package de.game.gridlogic;

import java.util.ArrayList;

import de.game.graph.Node;
import de.game.graph.NodeImpl;

public class PrioNodeQueue {

	private ArrayList<Node> prioQueue = new ArrayList<Node>();
	private Node platzhalter = new NodeImpl();
	private Node tempGesuchterNode = null;
	
	
	public PrioNodeQueue(){
		
	}
	
	
	public Node getMin(){
		return prioQueue.get(1);
	}
	
	
	public void delete(){
		if(!(prioQueue.size() == 1)){
		
		prioQueue.set(1, prioQueue.get(prioQueue.size()-1));
		prioQueue.remove(prioQueue.size()-1);
		int currentIndex = 1;
				
		while(true){			
			if(currentIndex*2 > prioQueue.size()-1){
				return;
			}
			
			if(currentIndex*2 == prioQueue.size()-1){
				if(prioQueue.get(2*currentIndex).getWeight() < prioQueue.get(currentIndex).getWeight()){
					switchElem(2*currentIndex, currentIndex);
					return;
				} 
				return;
			}
			
			
			if(prioQueue.get(2*currentIndex).getWeight() < prioQueue.get(currentIndex).getWeight() && !(prioQueue.get(2*currentIndex+1).getWeight() < prioQueue.get(currentIndex).getWeight())){
				switchElem(currentIndex, 2*currentIndex);
				currentIndex *= 2;
				continue;
			}
			
				if(!(prioQueue.get(2*currentIndex).getWeight() < prioQueue.get(currentIndex).getWeight()) && 
						prioQueue.get(2*currentIndex+1).getWeight() < prioQueue.get(currentIndex).getWeight()){
					switchElem(currentIndex, 2*currentIndex+1);
					currentIndex = currentIndex * 2 + 1;
					continue;
			}
			
			if(prioQueue.get(2*currentIndex).getWeight() < prioQueue.get(currentIndex).getWeight() 
					&& prioQueue.get(2*currentIndex+1).getWeight() < prioQueue.get(currentIndex).getWeight()){
				if(prioQueue.get(2*currentIndex).getWeight() < prioQueue.get(2*currentIndex+1).getWeight()){
					switchElem(currentIndex, 2*currentIndex);
					currentIndex *= 2;
					continue;
				} else {
					switchElem(currentIndex, 2*currentIndex+1);
					currentIndex = currentIndex * 2 + 1;
					continue;
				}
			}
			
			if(prioQueue.get(2*currentIndex).getWeight() >= prioQueue.get(currentIndex).getWeight() 
					&& prioQueue.get(2*currentIndex+1).getWeight() >= prioQueue.get(currentIndex).getWeight()){
				return;
			}
			
			
		}}
	}
	
	
	
	public void reshuffleQueue(){
		ArrayList<Node> neu = new ArrayList<Node>();
		for(int i = 1; i < prioQueue.size(); i++){
			neu.add(prioQueue.get(i));			
		}
		prioQueue.clear();
		for(Node a : neu){
			add(a);
		}
		
	}
	
//	public void deleteElement(Node n){
//		System.out.println("index des elements: "+prioQueue.indexOf(n));
////		prioQueue.add(prioQueue.get(1));
////		prioQueue.remove(n);
//	}
	
	
	public boolean isEmpty(){
		return (prioQueue.size() == 1);
	}
	
	
	public void add(Node a){
		if(prioQueue.isEmpty()){
			prioQueue.add(platzhalter);
			prioQueue.add(a);
		} else {
			prioQueue.add(a);
			int currentIndex = prioQueue.size()-1;
			
			while(a.getWeight() < prioQueue.get(currentIndex/2).getWeight()){
				switchElem(currentIndex, currentIndex/2);
				currentIndex /= 2;
			}				
		}
	}
	
	
	private void switchElem(int a, int b){
	
		Node node1 = prioQueue.get(a);
		prioQueue.set(a, prioQueue.get(b));
		prioQueue.set(b, node1);
	
	}
	
	public boolean contains(Node n){
		for(int i = 1; i < prioQueue.size(); i++){
			if(n.getName().equals(prioQueue.get(i).getName())){
				tempGesuchterNode = prioQueue.get(i);
				return true;
			}
		}
		return false;
	}
	
	
	public String toString(){
		String t = "";
		for(int i = 1; i < prioQueue.size(); i++){
			t = t + prioQueue.get(i).getName()+ "-"+prioQueue.get(i).getWeight()+ "||";
		}
		return t;
	}


	public Node getTempGesuchterNode() {
		return tempGesuchterNode;
	}


	public void setTempGesuchterNode(Node tempGesuchterNode) {
		this.tempGesuchterNode = tempGesuchterNode;
	}


	public void clear() {
		prioQueue.clear();
		
	}
	
	
	
}
	